{extend name="common@base" /}
{block name="content"}
<link rel="stylesheet" href="__CSS__/jstree/style.css">
<section class="content">
    <div id="jstree_demo"></div>
</section>
{/block}
{block name="page_script"}
<script>
  require(["layer_config", "jstree", "jstree.contextmenu", "jstree.dnd"], function (layer) {
    $(function () {
      $("#jstree_demo").on("loaded.jstree", function (e, data) {
        data.instance.open_all();
      }).jstree({
        "core": {
          "animation": 1,
          "check_callback": true,
          "themes": {"stripes": true},
          "expand_selected_onload": true,
          'data': {
            'url': function (node) {
              return node.id === '#' ?
                '{:url("base/department/treedepartment")}' : 'ajax_demo_children.json';
            },
            'data': function (node) {
              return {'id': node.id};
            }
          }
        },
        "plugins": [
          "contextmenu", "dnd", "search",
          "state", "types", "wholerow"
        ],
        "state": {"key": "jstree_demo"},
        "contextmenu": {
          items: {
            "create": {
              "separator_before": false,
              "separator_after": true,
              "_disabled": false, //(this.check("create_node", data.reference, {}, "last")),
              "label": "新建",
              "action": function (data) {
                var inst = $.jstree.reference(data.reference),
                  obj = inst.get_node(data.reference);
                inst.create_node(obj, {}, "last", function (new_node) {
                  setTimeout(function () {
                    inst.edit(new_node);
                  }, 0);
                });
              }
            },
            "edit": {
              "separator_before": false,
              "separator_after": false,
              "_disabled": false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
              "label": "修改",
              "action": function (data) {
                var inst = $.jstree.reference(data.reference),
                  obj = inst.get_node(data.reference);
                console.log(obj);
                window.location.href = "{:url('edit')}?id=" + obj.id;
              }
            },
            "remove": {
              "separator_before": false,
              "icon": false,
              "separator_after": false,
              "_disabled": false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
              "label": "删除",
              "action": function (data) {
                var inst = $.jstree.reference(data.reference),
                  obj = inst.get_node(data.reference);
                $.post("{:url('delete')}", {
                  id: obj.id,
                }, function (result) {
                  if (result.code == 1) {
                    if (inst.is_selected(obj)) {
                      inst.delete_node(inst.get_selected());
                    } else {
                      inst.delete_node(obj);
                    }
                    js_message(result,true);
                  }else{
                    js_alert(result);

                  }
                }, 'json');
              }
            },
          }
        },
      });
      $("#jstree_demo").on("create_node.jstree", function (e, data) {
        var node = data.node;
        window.location.href = "{:url('edit')}?id=" + node.id + "&parent_id=" + node.parent;
      });
    });
  });


</script>
{/block}





